<?php

// load configuration file
require_once('../config/config.php');
require_once('../config/error_handler.php');

// Chat class that contains server-side chat functionality
class Profiel {

    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct() {
        // connect to the database
        $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    }

    // destructor closes database connection
    public function __destruct() {
        $this->mMysqli->close();
    }

    //The postMessages method inserts a profile into the database

    public function postMessage($titel, $tekst, $link) {

        // escape the variable data for safely adding them to the database
        $titel = $this->mMysqli->real_escape_string($titel);
        $tekst = $this->mMysqli->real_escape_string($tekst);
        $link = $this->mMysqli->real_escape_string($link);

        // build the SQL query that adds a new message to the server
        $query = 'INSERT INTO nieuws(datum, titel, tekst, link) ' .
                'VALUES (NOW(), "' . $titel . '","' . $tekst . '","' . $link . '")';
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo "Nieuws is verplaats.<br/>";
        echo "<a href=\"adminSuccess.php\">terug naar nieuws berwerken pagina...</a>";
    }

    public function retrieveAllNews() {
        $query = "SELECT * FROM nieuws ORDER BY id_nieuws DESC ";
        // execute the query
        $result = $this->mMysqli->query($query);

        // build the JSON response
        $response = array();
        $response['messages'] = array();

        // check to see if we have any results
        if ($result->num_rows) {
            // loop through all the fetch
            while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $message = array();
                $message['id_nieuws'] = $row['id_nieuws'];
                $message['titel'] = $row['titel'];
                $message['tekst'] = $row['tekst'];
                $message['link'] = $row['link'];
                $message['datum'] = $row['datum'];
                array_push($response['messages'], $message);
            }
            // close the database connection as soon as possible
            $result->close();
        }
        // return the JSON response
        return $response;
    }

    public function deleteNieuws($id) {

        // escape the variable data for safely adding them to the database
        $titel = $this->mMysqli->real_escape_string($id);

        // build the SQL query that adds a new message to the server
        $query = 'DELETE FROM nieuws WHERE id_nieuws=' . $id;
        // execute the SQL query
        $result = $this->mMysqli->query($query);
        echo "Nieuws is verwijderd.<br/>";
        echo "<a href=\"adminSuccess.php\">terug naar nieuws berwerken pagina...</a>";
    }

}

?>
